package com.bskj.common.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.bskj.common.bean.vo.CCBNoSenseParkingInReqVo;
import com.bskj.common.bean.vo.CCBNoSenseParkingOutReqVo;
import com.bskj.common.bean.vo.NoSenseParkingInReqVo;
import com.bskj.common.bean.vo.NoSenseParkingOutReqVo;
import com.bskj.common.bean.vo.NoSenseParkingUserStatusReqVo;

public class NoSensePayUtils {
	private static final String NO_SENSE_PAY_HTTPS_URL = "https://wx.91xft.cn/transaction/parking";
	
	private static final String JH_NO_SENSE_PAY_PARK_IN_HTTPS_URL = "https://xlink.91xft.cn/parking/aggegation/entry";
	private static final String JH_NO_SENSE_PAY_PARK_OUT_HTTPS_URL = "https://xlink.91xft.cn/parking/aggegation/entrance";

	public static Map<String, Object> JHParkIn(CCBNoSenseParkingInReqVo reqVo) throws Exception {
		JSONObject paramMap = new JSONObject();
		paramMap.put("app_id", reqVo.getAppId());
		paramMap.put("merchant_code", reqVo.getMerchantCode());
		paramMap.put("parking_flag", reqVo.getParkingFlag());
		paramMap.put("channel", reqVo.getChannel());
		if (!StringUtils.isBank(reqVo.getExtra())) {
			paramMap.put("extra", reqVo.getExtra());
		}
		paramMap.put("trade_scene", reqVo.getTradeScene());
		paramMap.put("plate_number", reqVo.getPlateNumber());
		paramMap.put("start_time", reqVo.getStartTime());
		paramMap.put("car_type", reqVo.getCarType());
		paramMap.put("free_time", reqVo.getFreeTime());
		paramMap.put("sign_type", reqVo.getSignType());

		String src = "";
		List<String> keyList = new ArrayList<>();
		for (String key : paramMap.keySet()) {
			keyList.add(key);
		}
		Collections.sort(keyList);
		for (String key : keyList) {
			src += (key + "=" + paramMap.get(key) + "&");
		}
		src = src.substring(0, src.length() - 1);
		String afterSignStr = DigestUtil.digestKeyValue(reqVo.getSignKey(), src);
		paramMap.put("sign", afterSignStr);

		LogUtils.info("JH in 无感 reqUrl : " + JH_NO_SENSE_PAY_PARK_IN_HTTPS_URL);
		LogUtils.info("JH in 无感 reqParam : " + paramMap.toJSONString());
		
		String str = HttpClientUtils.doPost(JH_NO_SENSE_PAY_PARK_IN_HTTPS_URL, paramMap.toJSONString(), "UTF-8");

		LogUtils.info("JH in 无感 respone : " + str);
		JSONObject responseObj = JSONObject.parseObject(str);
		return responseObj;
	}

	public static Map<String, Object> JHParkOut(CCBNoSenseParkingOutReqVo outReqVo) throws Exception {
		JSONObject paramMap = new JSONObject();
		paramMap.put("app_id", outReqVo.getAppId());
		paramMap.put("merchant_code", outReqVo.getMerchantCode());
		paramMap.put("parking_flag", outReqVo.getParkingFlag());
		paramMap.put("channel", outReqVo.getChannel());
		if (!StringUtils.isBank(outReqVo.getExtra())) {
			paramMap.put("extra", outReqVo.getExtra());
		}
		paramMap.put("trade_scene", outReqVo.getTradeScene());
		paramMap.put("amount", outReqVo.getAmount());
		paramMap.put("notify_url", outReqVo.getNotifyUrl());
		paramMap.put("start_time", outReqVo.getStartTime());
		paramMap.put("end_time", outReqVo.getEndTime());
		paramMap.put("charging_time", outReqVo.getChargingTime());
		paramMap.put("plate_number", outReqVo.getPlateNumber());
		paramMap.put("car_type", outReqVo.getCarType());
		paramMap.put("body", outReqVo.getBody());
		paramMap.put("subject", outReqVo.getSubject());
		paramMap.put("remark", outReqVo.getRemark());
		paramMap.put("sign_type", outReqVo.getSignType());
		paramMap.put("charging_time", outReqVo.getChargingTime());

		String src = "";
		List<String> keyList = new ArrayList<>();
		for (String key : paramMap.keySet()) {
			keyList.add(key);
		}
		Collections.sort(keyList);
		for (String key : keyList) {
			src += (key + "=" + paramMap.get(key) + "&");
		}
		src = src.substring(0, src.length() - 1);
		String afterSignStr = DigestUtil.digestKeyValue(outReqVo.getSignKey(), src);
		paramMap.put("sign", afterSignStr);

		LogUtils.info("JH out 无感 reqUrl : " + JH_NO_SENSE_PAY_PARK_OUT_HTTPS_URL);
		LogUtils.info("JH out 无感 reqParam : " + paramMap.toJSONString());

		String str = HttpClientUtils.doPost(JH_NO_SENSE_PAY_PARK_OUT_HTTPS_URL, paramMap.toJSONString(), "UTF-8");
		LogUtils.info("JH out 无感 respone : " + str);
		JSONObject responseObj = JSONObject.parseObject(str);
		return responseObj;
	}

	public static Map<String, Object> parkIn(NoSenseParkingInReqVo inReqVo) throws Exception {
		Map<String, String> paramMap = new HashMap<>();
		paramMap.put("appId", inReqVo.getAppid());
		paramMap.put("timestamp", inReqVo.getTimestamp());
		paramMap.put("type", inReqVo.getType());
		paramMap.put("method", inReqVo.getMethod());
		paramMap.put("signType", inReqVo.getSignType());
		paramMap.put("notifyUrl", inReqVo.getNotifyUrl());

		JSONObject dataJson = new JSONObject();
		dataJson.put("tradeScene", inReqVo.getTradeScene());
		dataJson.put("startTime", inReqVo.getStartTime());
		dataJson.put("plateNumber", inReqVo.getPlateNumber());
		dataJson.put("carType", inReqVo.getCarType());
		dataJson.put("freeTime", inReqVo.getFreeTime());
		dataJson.put("parkingName", inReqVo.getParkingName());
		dataJson.put("mchId", inReqVo.getMchId());
		String data = EncryptUtil.enBase64(dataJson.toJSONString());
		paramMap.put("data", data);

		String src = "";
		List<String> keyList = new ArrayList<>();
		for (String key : paramMap.keySet()) {
			keyList.add(key);
		}
		Collections.sort(keyList);
		for (String key : keyList) {
			src += (key + "=" + paramMap.get(key) + "&");
		}
		src = src.substring(0, src.length() - 1);
		String afterSignStr = DigestUtil.digestKeyValue(inReqVo.getSign(), src);
		paramMap.put("sign", afterSignStr);

		String str = HttpClientUtils.doPost(NO_SENSE_PAY_HTTPS_URL, paramMap, "UTF-8");
		JSONObject responseObj = JSONObject.parseObject(str);
		return responseObj;
	}

	public static Map<String, Object> queryUserStatus(NoSenseParkingUserStatusReqVo userstatusReqVo) throws Exception {
		Map<String, String> paramMap = new HashMap<>();
		paramMap.put("appId", userstatusReqVo.getAppid());
		paramMap.put("timestamp", userstatusReqVo.getTimestamp());
		paramMap.put("type", userstatusReqVo.getType());
		paramMap.put("method", userstatusReqVo.getMethod());
		paramMap.put("signType", userstatusReqVo.getSignType());
		paramMap.put("notifyUrl", userstatusReqVo.getNotifyUrl());

		JSONObject dataJson = new JSONObject();
		dataJson.put("tradeScene", userstatusReqVo.getTradeScene());
		dataJson.put("subOpenId", userstatusReqVo.getSubOpenId());
		dataJson.put("jumpScene", userstatusReqVo.getJumpScene());
		dataJson.put("plateNumber", userstatusReqVo.getPlateNumber());
		dataJson.put("mchId", userstatusReqVo.getMchId());
		String data = EncryptUtil.enBase64(dataJson.toJSONString());
		paramMap.put("data", data);

		String src = "";
		List<String> keyList = new ArrayList<>();
		for (String key : paramMap.keySet()) {
			keyList.add(key);
		}
		Collections.sort(keyList);
		for (String key : keyList) {
			src += (key + "=" + paramMap.get(key) + "&");
		}
		src = src.substring(0, src.length() - 1);
		String afterSignStr = DigestUtil.digestKeyValue(userstatusReqVo.getSign(), src);
		paramMap.put("sign", afterSignStr);
		
		System.out.println(paramMap.toString());

		
		long startTime = System.currentTimeMillis();
		String str = HttpClientUtils.doPost(NO_SENSE_PAY_HTTPS_URL, paramMap, "UTF-8");
		long endTime = System.currentTimeMillis();
		System.out.println("无感耗时" + (endTime - startTime) + "ms");
		JSONObject responseObj = JSONObject.parseObject(str);
		return responseObj;
	}

	public static void main(String[] args) throws Exception {
		//								NoSenseParkingInReqVo inReqVo = new NoSenseParkingInReqVo("1032160436943847424","WECHAT","MD5","89EF2D91355AE1C4498B213411659A3C");
		//								inReqVo.setTradeScene("PARKING");
		//								inReqVo.setStartTime(DateTimeUtil.date2String(new Date(), "yyyyMMddHHmmss"));
		//								inReqVo.setFreeTime("0");
		//								inReqVo.setPlateNumber("湘G36953");
		//								inReqVo.setCarType("小型车");
		//								inReqVo.setParkingName("欢乐海岸停车场");
		//								inReqVo.setNotifyUrl("https://kuaifu.boshukeji.com/api/noSensePayNotify");
		//								inReqVo.setMchId("1510658731");
		//								Map<String,Object> respone = parkIn(inReqVo);
		//								System.out.println(respone);
		//								if((boolean)respone.get("result")) {
		//									String data = EncryptUtil.deBase64((String)respone.get("data"));
		//									JSONObject dataJson = JSONObject.parseObject(data);
		//									System.out.println(data);
		//								}

		//				NoSenseParkingOutReqVo outReqVo = new NoSenseParkingOutReqVo("1032160436943847424", "WECHAT", "MD5",
		//						"89EF2D91355AE1C4498B213411659A3C");
		//				outReqVo.setAmount("1");
		//				outReqVo.setClientIp("127.0.0.1");
		//				outReqVo.setTradeScene("PARKING");
		//				outReqVo.setStartTime(DateTimeUtil.date2String(new Date(), "yyyyMMddHHmmss"));
		//				outReqVo.setEndTime(DateTimeUtil.date2String(new Date(), "yyyyMMddHHmmss"));//DateTimeUtil.date2String(new Date(), "yyyyMMddHHmmss")
		//				outReqVo.setPlateNumber("湘G36953");
		//				outReqVo.setCarType("小型车");
		//				outReqVo.setParkingName("湘腾停车场");
		//				outReqVo.setNotifyUrl("https://kuaifu.boshukeji.com/api/noSensePayNotify");
		//				outReqVo.setMchId("1510658731");
		//		
		//				Map<String, Object> respone = parkOut(outReqVo);
		//				System.out.println(respone);
		//				if ((boolean) respone.get("result")) {
		//					String data = EncryptUtil.deBase64((String) respone.get("data"));
		//					JSONObject dataJson = JSONObject.parseObject(data);
		//					if ("SUCCESS".equals(dataJson.get("isAllow"))) {
		//						System.out.println("出行！！！");
		//					}
		//					System.out.println(data);
		//				}

//				CCBNoSenseParkingInReqVo inReqVo = new CCBNoSenseParkingInReqVo("1052864091804139520", "1130300000261", "1087534454613737472", "湘G36953", DateTimeUtil.date2String(new Date(), "yyyyMMddHHmmss"), 30*60 + "", "MD5", "62C433D13B689A91FAE54C5E1CC4C954");
//				long startIme = System.currentTimeMillis();
//				Map<String, Object> respone = JHParkIn(inReqVo);
//				System.out.println(respone);
//				long endTime = System.currentTimeMillis();
//				System.out.println("JHParkIn cost : " + (endTime - startIme) + "ms");

//		CCBNoSenseParkingOutReqVo outReqVo = new CCBNoSenseParkingOutReqVo("1052864091804139520", "1130300000261",
//				"1087534454613737472", "1", "https://kuaifu.boshukeji.com/pay/noSenseNotify",
//				DateTimeUtil.date2String(new Date(), "yyyyMMddHHmmss"),
//				DateTimeUtil.date2String(new Date(), "yyyyMMddHHmmss"), 30 * 60 + "", "湘AH97M7", "星富通停车场", "MD5",
//				"62C433D13B689A91FAE54C5E1CC4C954");
//		long outstartIme = System.currentTimeMillis();
//		Map<String, Object> outrespone = JHParkOut(outReqVo);
//		long outendTime = System.currentTimeMillis();
//		System.out.println(outrespone);
//		JSONObject data = JSONObject.parseObject(outrespone.get("data").toString());
//		System.out.println(data.getString("is_allow"));
//		System.out.println("JHParkOut cost : " + (outendTime - outstartIme) + "ms");
				
				
				NoSenseParkingUserStatusReqVo userStatusReqVo = new NoSenseParkingUserStatusReqVo("1012526397677109248",
						"WECHAT", "MD5", "F3B2E91B5689E63AA34465FA7D33DFEC");
				userStatusReqVo.setNotifyUrl("https://kuaifu.boshukeji.com/api/noSensePayNotify");
				userStatusReqVo.setTradeScene("PARKING");
				userStatusReqVo.setMchId("1508738031");
				userStatusReqVo.setSubOpenId("o40OG5M0cMRh2HEA3sekWEpk--IE");
				userStatusReqVo.setPlateNumber(null);
				Map<String, Object> respone = queryUserStatus(userStatusReqVo);
				System.out.println(respone);
	}

	public static Map<String, Object> parkOut(NoSenseParkingOutReqVo outReqVo) throws Exception {
		Map<String, String> paramMap = new HashMap<>();
		paramMap.put("appId", outReqVo.getAppid());
		paramMap.put("timestamp", outReqVo.getTimestamp());
		paramMap.put("type", outReqVo.getType());
		paramMap.put("method", outReqVo.getMethod());
		paramMap.put("signType", outReqVo.getSignType());
		paramMap.put("notifyUrl", outReqVo.getNotifyUrl());

		JSONObject dataJson = new JSONObject();
		dataJson.put("amount", outReqVo.getAmount());
		dataJson.put("clientIp", outReqVo.getClientIp());
		dataJson.put("tradeScene", outReqVo.getTradeScene());
		dataJson.put("startTime", outReqVo.getStartTime());
		dataJson.put("endTime", outReqVo.getEndTime());
		dataJson.put("chargingTime", outReqVo.getChargingTime());
		dataJson.put("plateNumber", outReqVo.getPlateNumber());
		dataJson.put("carType", outReqVo.getCarType());
		dataJson.put("parkingName", outReqVo.getParkingName());
		dataJson.put("body", outReqVo.getBody());
		dataJson.put("subject", outReqVo.getSubject());
		dataJson.put("remark", outReqVo.getRemark());
		dataJson.put("mchId", outReqVo.getMchId());

		String data = EncryptUtil.enBase64(dataJson.toJSONString());
		paramMap.put("data", data);

		String src = "";
		List<String> keyList = new ArrayList<>();
		for (String key : paramMap.keySet()) {
			keyList.add(key);
		}
		Collections.sort(keyList);
		for (String key : keyList) {
			src += (key + "=" + paramMap.get(key) + "&");
		}
		src = src.substring(0, src.length() - 1);
		String afterSignStr = DigestUtil.digestKeyValue(outReqVo.getSign(), src);
		paramMap.put("sign", afterSignStr);

		long startTime = System.currentTimeMillis();
		String str = HttpClientUtils.doPost(NO_SENSE_PAY_HTTPS_URL, paramMap, "UTF-8");
		long endTime = System.currentTimeMillis();
		System.out.println("无感耗时" + (endTime - startTime) + "ms");
		JSONObject responseObj = JSONObject.parseObject(str);
		return responseObj;
	}

	public static Map<String, Object> queryOrder() throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		return resultMap;
	}
}
